package com.numberone.master.common.Interface;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.numberone.master.common.exception.BizException;
import com.numberone.master.common.menu.RespCode;
import com.numberone.master.common.utils.Encrypt;
import com.numberone.master.common.utils.HttpClientUtil;
import com.numberone.master.common.utils.JsonUtils;
import com.numberone.master.common.utils.StringUtils;
import com.numberone.master.modules.Interfaces.entity.FlowInfo;


/**
 * 常州移动接口
 * @author ck
 *
 * @date 2018年3月5日下午4:11:45
 */
public class YiDong {

	/**
	 * 常州账号
	 */
	private final static String APPID_CZ ="GWH32AM";
	private final static String PASSWORD_CZ ="WZY711";
	
	/**
	 * 吉林
	 */
	private final static String APPID_JL ="NM8A1HL";
	private final static String PASSWORD_JL ="NJSYZXX";
	
	/**
	 * 徐州
	 */
	private final static String APPID_XZ ="GSPO2AP";
	private final static String PASSWORD_XZ ="SYZ931";
	
	/**在线信息实时查询**/
	private final static String gprsrealsingle ="http://183.230.96.66:8087/v2/gprsrealsingle";
	/**用户状态信息实时查询**/
	private final static String userstatusrealsingle ="http://183.230.96.66:8087/v2/userstatusrealsingle";
	/**码号信息查询**/
	private final static String cardinfo ="http://183.230.96.66:8087/v2/cardinfo";
	/**用户当月GPRS查询**/
	private final static String gprsusedinfosingle ="http://183.230.96.66:8087/v2/gprsusedinfosingle";
	/**开关机信息实时查询**/
	private final static String onandoffrealsingle ="http://183.230.96.66:8087/v2/onandoffrealsingle";
	/**短信使用信息批量查询**/
	private final static String batchsmsusedbydate ="http://183.230.96.66:8087/v2/batchsmsusedbydate";
	/**流量使用信息批量查询**/
	private final static String batchgprsusedbydate ="http://183.230.96.66:8087/v2/batchgprsusedbydate";
	/**用户余额信息实时查询**/
	private final static String balancerealsingle ="http://183.230.96.66:8087/v2/balancerealsingle";
	/**用户当月短信查询**/
	private final static String smsusedinfosingle ="http://183.230.96.66:8087/v2/smsusedinfosingle";
	/**短信状态重置**/
	private final static String smsstatusreset ="http://183.230.96.66:8087/v2/smsstatusreset";
	/**集团用户数查询**/
	private final static String groupuserinfo ="http://183.230.96.66:8087/v2/groupuserinfo";
	/**用户短信使用查询**/
	private final static String smsusedbydate ="http://183.230.96.66:8087/v2/smsusedbydate";
	/**套餐内GPRS流量使用情况实时查询**/
	private final static String gprsrealtimeinfo ="http://183.230.96.66:8087/v2/gprsrealtimeinfo";
	
	/** 开机功能 **/
	private final static String stopOpenSubs ="http://183.230.96.66:8087/v2/stopOpenSubs";

	
	/**
	 * @param EBID
	 * @param type 1：常州  2：吉林  3：徐州
	 * @return
	 */
	public static String yiDongPost(String EBID,String type,String iCCID,String transid){
		String url = "";
		String param = "";
		String appid = "";
		String password = "";
		String res = "";
		String status = "";
		if(StringUtils.isEmpty(EBID) || StringUtils.isEmpty(type) || StringUtils.isEmpty(iCCID) || StringUtils.isEmpty(transid)){
			//throw new BizException(RespCode.NOT_PARAM_ERROR);
		}else{
			//封装参数
			if(StringUtils.equals(type, "1")){
				appid = APPID_CZ;
				password = PASSWORD_CZ;
			}
			if(StringUtils.equals(type, "2")){
				appid = APPID_JL;
				password = PASSWORD_JL;
			}
			if(StringUtils.equals(type, "3")){
				appid = APPID_XZ;
				password = PASSWORD_XZ;
			}
			switch(EBID){
			case "0001000000008":
				url = gprsrealsingle+"?";
			    break;
			case "0001000000009":
				url = userstatusrealsingle+"?";
			    break;
			case "0001000000010":
				url = cardinfo+"?";
			    break;
			case "0001000000012":
				url = gprsusedinfosingle+"?";
			    break;
			case "0001000000025":
				url = onandoffrealsingle+"?";
			    break;
			case "0001000000026":
				url = batchsmsusedbydate+"?";
			    break;
			case "0001000000027":
				url = batchgprsusedbydate+"?";
			    break;
			case "0001000000035":
				url = balancerealsingle +"?";
			    break;
			case "0001000000036":
				url = smsusedinfosingle +"?";
			    break;
			case "0001000000034":
				url = smsstatusreset  +"?";
			    break;
			case "0001000000039":
				url = groupuserinfo +"?";
			    break;
			case "0001000000040":
				url = smsusedbydate+"?";
			    break;
			case "0001000000083":
				url = gprsrealtimeinfo +"?";
				break;
			 default:
				// throw new BizException("0089","没有此EBID接口业务");
			}
			param = "appid="+appid+"&ebid="+EBID+"&transid="+transid+"&token="+getCMCCToken(appid,password,transid)+"&iccid="+iCCID;
			String getUrl = url+param;
			res = HttpClientUtil.sendGet(getUrl);
			System.out.println("物联网卡数据返回==================================");
			System.out.println(res);
			Map<String,Object> msgMap = JsonUtils.fromJson(res);
			System.out.println(msgMap);
			status = (String)msgMap.get("status");
/*			if(!StringUtils.equals("0", status)){
				String message = (String)msgMap.get("message");
				throw new BizException("0090",message);
			}*/
		}
		return res;
	}
	
	/**
	 * 获取token
	 * @param appid appid
	 * @param password 密码
	 * @param transid
	 * @return
	 */
	private static String getCMCCToken(String appid,String password,String transid) {
		Encrypt encrypt=new Encrypt();
		String sourceStr = appid+password+transid;
		String res = encrypt.SHA256(sourceStr);
		return res;
	}
	
	/**
	 * 查询卡的余额
	 * @param numberId iccid或者卡的id
	 * @param areaId 所属的的地区
	 * @param transid 生成的transid
	 * @return
	 */
	public static String getBalance(String numberId,String areaId,String transid){
		String balance = "0.00";
		if(StringUtils.isBlank(numberId) || StringUtils.isBlank(areaId) || StringUtils.isBlank(transid)){
			throw new BizException(RespCode.NOT_PARAM_ERROR);
		}
		//青岛卡
		if(StringUtils.equals(areaId, "5")){
			String res = ShanDong.qrySubsBalance(numberId);
			System.out.println(res);
			JSONObject jsonobj = new JSONObject(res);
			Object responseHeader = jsonobj.get("responseHeader");
			JSONObject responseHeaderObj = new JSONObject(responseHeader.toString());
			Object resultCode = responseHeaderObj.get("resultCode");
			if(StringUtils.equals("000000", resultCode.toString())){
				Object balanceObj = jsonobj.get("balance");
				balance = balanceObj.toString();
			}
		}else{
			String res = yiDongPost("0001000000035", areaId, numberId, transid);
			JSONObject jsonobj = new JSONObject(res);
			Object result = jsonobj.get("result");
			Object status = jsonobj.get("status");
			if(StringUtils.equals(status.toString(), "0")){
				JSONArray jsonArray = null;
				jsonArray = new JSONArray(result.toString());
				Object balanceObj = jsonArray.getJSONObject(0).get("balance");
				balance = balanceObj.toString();
			}
		}
		return balance;
	}
	
	/**
	 * 查询实时流量使用详情
	 * @param numberId iccid或者卡的id
	 * @param areaId 所属的的地区
	 * @param transid 生成的transid
	 * @return
	 */
	public static Map<String,Object> getGPRSInfo(String numberId,String areaId,String transid){
		Map<String,Object> map = new HashMap<String, Object>();
		String res = "";
		if(StringUtils.isBlank(numberId) || StringUtils.isBlank(areaId) || StringUtils.isBlank(transid)){
			throw new BizException(RespCode.NOT_PARAM_ERROR);
		}
		//青岛卡
		if(StringUtils.equals(areaId, "5")){
			 res = ShanDong.qryGPRSRealTimeInfo(numberId,"1","531");
		}else{
			res = yiDongPost("0001000000083", areaId, numberId, transid);
		}
			JSONObject jsonobj = new JSONObject(res);
			Object status = jsonobj.get("status");
			if(StringUtils.equals(status.toString(), "0")){
				Object result = jsonobj.get("result");
				JSONArray jsonArray = null;
		        jsonArray = new JSONArray(result.toString());
		        Object gprsObj = jsonArray.getJSONObject(0).get("gprs");
		        JSONArray gprsArray = null;
		        gprsArray = new JSONArray(gprsObj.toString());
				Object total = gprsArray.getJSONObject(0).get("total");//订购时间
				Object left = gprsArray.getJSONObject(0).get("left");//生效时间
				Object used = gprsArray.getJSONObject(0).get("used");//失效时间
				map.put("total", total);//总量
				map.put("left", left);//剩余
				map.put("used", used);//已使用量
			}
		return map;
	}
	
	public static String getPackName(String numberId,String areaId,String transid){
		String res = "";
		String packName = "";
		if(StringUtils.isBlank(numberId) || StringUtils.isBlank(areaId) || StringUtils.isBlank(transid)){
			throw new BizException(RespCode.NOT_PARAM_ERROR);
		}
		//青岛卡
		if(StringUtils.equals(areaId, "5")){
			 res = ShanDong.qryGPRSRealTimeInfo(numberId,"1","531");
		}else{
			res = yiDongPost("0001000000083", areaId, numberId, transid);
		}
		if(!StringUtils.isBlank(res)){
			JSONObject jsonobj = new JSONObject(res);
			Object status = jsonobj.get("status");
			if(StringUtils.equals(status.toString(), "0")){
				Object result = jsonobj.get("result");
				JSONArray jsonArray = null;
		        jsonArray = new JSONArray(result.toString());
		        Object gprsObj = jsonArray.getJSONObject(0).get("gprs");
		        JSONArray gprsArray = null;
		        gprsArray = new JSONArray(gprsObj.toString());
				Object packname = gprsArray.getJSONObject(0).get("prodname");//套餐名称
				packName = packname.toString();
			}
		}
		return packName;
	}
	
	/**
	 * @param numberId
	 * @param areaId
	 * @param transid
	 * @return
	 */
	public static FlowInfo getPackInfo(String numberId,String areaId,String transid){
		FlowInfo flowInfo = new FlowInfo();
		String res = "";
		if(StringUtils.isBlank(numberId) || StringUtils.isBlank(areaId) || StringUtils.isBlank(transid)){
			throw new BizException(RespCode.NOT_PARAM_ERROR);
		}
		//青岛卡
		if(StringUtils.equals(areaId, "5")){
			String userState = getUserState(numberId, areaId, transid);
			String cardState = getCardState(numberId, areaId, transid);
			flowInfo.setUserState(userState);
			flowInfo.setCardState(cardState);
			res = ShanDong.qrySubsPackage(numberId);
			JSONObject jsonobj = new JSONObject(res);
			Object responseHeader = jsonobj.get("responseHeader");
			JSONObject responseHeaderObj = new JSONObject(responseHeader.toString());
			Object resultCode = responseHeaderObj.get("resultCode");
			if(StringUtils.equals("000000", resultCode.toString())){
				JSONArray jsonArray = null;
				Object result = jsonobj.get("packageVos");
				jsonArray = new JSONArray(result.toString());
				String packName = getPackName(numberId, areaId, transid);
				if(!StringUtils.isBlank(packName)){
					//添加套餐信息
					Map<String,Object> gpsMap = getGPRSInfo(numberId, areaId, transid);
					flowInfo.setTotal(gpsMap.get("total").toString());//总量
					flowInfo.setLeft(gpsMap.get("left").toString());//剩余
					flowInfo.setUsed(gpsMap.get("used").toString());//已使用量
					for (java.util.Iterator tor=jsonArray.iterator();tor.hasNext();) {
						JSONObject job = (JSONObject)tor.next();
						String orderingtime = job.get("orderingtime").toString();
						String starttime = job.get("starttime").toString();
						String deadtime = job.get("deadtime").toString();
						String bunessname = job.get("bunessname").toString();
						if(bunessname.equals(packName)){
							flowInfo.setBunessname(bunessname);
							flowInfo.setOrderingtime(orderingtime);
							flowInfo.setStarttime(starttime);
							flowInfo.setDeadtime(deadtime);
							//获取现在的时间
							String nowTime = StringUtils.getDateDay();
							//计算起止结束时间
							if(!StringUtils.isBlank(starttime)){
								String useDay = StringUtils.getStringDayNum(starttime,nowTime);//使用时长
								flowInfo.setUse_day(useDay);
							}else{
								flowInfo.setUse_day("--个月--天");
							}
							if(!StringUtils.isBlank(starttime)){
								String surplusDay = StringUtils.getStringDayNum(nowTime,deadtime);//剩余时长
								flowInfo.setSurplus_day(surplusDay);
							}else{
								flowInfo.setSurplus_day("--个月--天");
							}
						}
					}
				}
			}
		}else{
			//暂无接口
			}
		return flowInfo;
	}
	
	/**
	 * 查询卡状态
	 * @param numberId
	 * @param areaId
	 * @param transid
	 * @return
	 */
	public static String getCardState(String numberId,String areaId,String transid){
		String cardState = ""; 
		if(StringUtils.isBlank(numberId) || StringUtils.isBlank(areaId) || StringUtils.isBlank(transid)){
			throw new BizException("-1","参数错误");
		}
		//青岛卡
		if(StringUtils.equals(areaId, "5")){
			//用户状态
			String cardRes = ShanDong.qryCardStatusResult(numberId,"531");
			System.out.println(cardRes);
			JSONObject jsonobj = new JSONObject(cardRes);
			Object statusObj = jsonobj.get("status");
			if(StringUtils.equals("0", statusObj.toString())){
				JSONArray jsonArray = null;
				Object result = jsonobj.get("result");
				jsonArray = new JSONArray(result.toString());
				Object status = jsonArray.getJSONObject(0).get("status");
				cardState = status.toString();
			}
		}else{
			//暂无接口
			}
		return cardState;
	}
	
	/**
	 * 查询用户状态
	 * @param numberId
	 * @param areaId
	 * @param transid
	 * @return
	 */
	public static String getUserState(String numberId,String areaId,String transid){
		String userState = ""; 
		if(StringUtils.isBlank(numberId) || StringUtils.isBlank(areaId) || StringUtils.isBlank(transid)){
			throw new BizException("-1","参数错误");
		}
		//青岛卡
		if(StringUtils.equals(areaId, "5")){
			//用户状态
			String userRes = ShanDong.qryUserStatusRealSingle(numberId,"531");
			System.out.println(userRes);
			JSONObject jsonobj = new JSONObject(userRes);
			Object statusObj = jsonobj.get("status");
			if(StringUtils.equals("0", statusObj.toString())){
				JSONArray jsonArray = null;
				Object result = jsonobj.get("result");
				jsonArray = new JSONArray(result.toString());
				Object status = jsonArray.getJSONObject(0).get("STATUS");
				userState = status.toString();
			}
		}else{
			//暂无接口
			}
		return userState;
	}
	
	
	/**
	 * 查询月流量使用情况
	 * @param numberId
	 * @param areaId
	 * @param transid
	 * @return
	 */
	public static String getMonthGprsInfo(String numberId,String areaId,String transid){
		String res = "";
		String totalGprs = "";
		if(StringUtils.isBlank(numberId) || StringUtils.isBlank(areaId) || StringUtils.isBlank(transid)){
			throw new BizException(RespCode.NOT_PARAM_ERROR);
		}
		//青岛卡
		if(StringUtils.equals(areaId, "5")){
			 res = ShanDong.qryCurMonthGprsUsedInfo(numberId,"1","531");
		}else{
			//res = yiDongPost("0001000000083", areaId, numberId, transid);
		}
		if(!StringUtils.isBlank(res)){
			JSONObject jsonobj = new JSONObject(res);
			Object status = jsonobj.get("status");
			if(StringUtils.equals(status.toString(), "0")){
				Object result = jsonobj.get("result");
				JSONArray jsonArray = null;
		        jsonArray = new JSONArray(result.toString());
				Object totalGprsObj = jsonArray.getJSONObject(0).get("total_gprs");//套餐名称
				totalGprs = totalGprsObj.toString();
			}
		}
		return totalGprs;
	}
	
}
